package io.github.jsp.service.impl;

import io.github.jsp.dao.JobLogMapper;
import io.github.jsp.model.JobLog;
import io.github.jsp.service.JobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class JobLogServiceImpl implements JobLogService {
    
    @Autowired
    private JobLogMapper jobLogMapper;
    
    @Override
    public JobLog getById(Long id) {
        return jobLogMapper.selectById(id);
    }
    
    @Override
    public List<JobLog> getByJobId(Integer jobId) {
        return jobLogMapper.selectByJobId(jobId);
    }
    
    @Override
    public List<JobLog> getWithPage(int page, int size) {
        int offset = (page - 1) * size;
        return jobLogMapper.selectWithPage(offset, size);
    }
    
    @Override
    public int getTotalCount() {
        return jobLogMapper.countAll();
    }
    
    @Override
    public List<JobLog> getByJobIdWithPage(Integer jobId, int page, int size) {
        int offset = (page - 1) * size;
        return jobLogMapper.selectByJobIdWithPage(jobId, offset, size);
    }
    
    @Override
    public int getCountByJobId(Integer jobId) {
        return jobLogMapper.countByJobId(jobId);
    }
    
    @Override
    public Long insert(JobLog jobLog) {
        jobLogMapper.insert(jobLog);
        return jobLog.getId();
    }
    
    @Override
    public int updateHandleInfo(JobLog jobLog) {
        return jobLogMapper.updateHandleInfo(jobLog);
    }
    
    @Override
    public int deleteById(Long id) {
        return jobLogMapper.deleteById(id);
    }
    
    @Override
    public int deleteByJobId(Integer jobId) {
        return jobLogMapper.deleteByJobId(jobId);
    }
    
    @Override
    public int clearOldLogs(int days) {
        long beforeTime = System.currentTimeMillis() - (days * 24L * 60 * 60 * 1000);
        return jobLogMapper.deleteBeforeTime(new Timestamp(beforeTime));
    }
}